package com.lijiajia.cloud.common.core.business;

import java.util.Collection;

/**
 * 业务异常
 *
 * @author lijiajia
 * @since lijiajia
 */
public class BusinessException extends RuntimeException {
    private static final String MESSAGE_STR = "[CODE: %s], [MESSAGE: %s]";
    private final String code;

    public BusinessException(String code, String msg) {
        super(msg);
        this.code = code;
    }

    /**
     * 携带固定消息
     *
     * @param status 状态枚举
     */
    public BusinessException(BusinessStatus status) {
        super(String.format(MESSAGE_STR, status.code(), status.msg()));
        this.code = status.code();
    }

    /**
     * 携带自定义异常消息
     *
     * @param status 接口状态枚举
     * @param msg    自定义异常消息
     */
    public BusinessException(BusinessStatus status, String msg) {
        super(String.format(MESSAGE_STR, status.code(), status.msg()));
        this.code = status.code();
    }

    /**
     * 格式化异常消息
     *
     * @param status 接口状态枚举
     * @param params 状态枚举中消息串参数
     */
    public BusinessException(BusinessStatus status, Collection params) {
        super(String.format(MESSAGE_STR, status.code(), String.format(status.msg(), params.toArray())));
        this.code = status.code();
    }

    /**
     * 格式化异常消息
     *
     * @param status 接口状态枚举
     * @param msg    异常消息格式串
     * @param params 异常消息串参数
     */
    public BusinessException(BusinessStatus status, String msg, Collection params) {
        super(String.format(MESSAGE_STR, status.code(), String.format(msg, params.toArray())));
        this.code = status.code();
    }
}
